#!/bin/bash

# 定义标记文件路径
INIT_MARKER="/initialized"

# 检查是否是首次启动
if [ -f "$INIT_MARKER" ]; then
    echo "非首次启动，跳过初始化命令"
else
  echo "首次启动，执行初始化命令..."

      # 这里替换为你的初始化命令
      echo "执行初始化操作"
      # 例如：初始化数据库、配置文件等
      # /path/to/your/init-command

      # 创建标记文件，标记已初始化
      touch "$INIT_MARKER"

      set -eux

      MYSQL_ROOT_PASSWORD=123
      MYSQL_SOCKET=/var/lib/mysql/mysql.sock

      # ========== 初始化 MySQL ==========
      if [ ! -d "/var/lib/mysql/mysql" ]; then
          echo "初始化 MySQL 数据库..."
          mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql
      fi
      MY_CNF="/etc/my.cnf"
      if ! grep -q "^bind-address" "$MY_CNF"; then
          echo "bind-address=0.0.0.0" >> "$MY_CNF"
      else
          sed -i 's/^bind-address.*/bind-address=0.0.0.0/' "$MY_CNF"
      fi
      # 临时启动 MySQL
      mysqld --user=mysql --datadir=/var/lib/mysql --socket=${MYSQL_SOCKET} --skip-networking &
      MYSQL_PID=$!

      # 等待 MySQL 启动完成
      for i in {30..0}; do
          if mysqladmin ping --socket=${MYSQL_SOCKET} --silent; then
              echo "MySQL 已启动"
              break
          fi
          echo '等待 MySQL 启动中...'
          sleep 1
      done

      # 设置 root 密码
      # 设置本地 root 密码
      mysql --socket=${MYSQL_SOCKET} -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; FLUSH PRIVILEGES;"

      # 创建允许任意主机访问的 root 用户
      mysql --socket=${MYSQL_SOCKET} -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';"
      mysql --socket=${MYSQL_SOCKET} -uroot -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;"
      mysql --socket=${MYSQL_SOCKET} -uroot -p${MYSQL_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;"
      echo "MySQL root 密码已设置"

      # ========== 初始化 Redis ==========
      REDIS_PASSWORD=123
      REDIS_CONF="/opt/redis6/redis.conf"

      # 设置 Redis 密码（如果配置文件中已有 requirepass 相关行则替换）
      if grep -q "^\s*#\?requirepass" "$REDIS_CONF"; then
          sed -i "s|^\s*#\?requirepass.*|requirepass ${REDIS_PASSWORD}|" "$REDIS_CONF"
      else
          echo "requirepass ${REDIS_PASSWORD}" >> "$REDIS_CONF"
      fi

      # ========== 初始化 Nacos ==========
      MYSQL_HOST=127.0.0.1
      MYSQL_PORT=3306
      NACOS_DB=nacos_config
      NACOS_USER=root
      NACOS_PASSWORD=123

      # 使用临时 MySQL 初始化 Nacos 数据库
      echo "初始化 Nacos 数据库..."
      mysql --socket=${MYSQL_SOCKET} -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE IF NOT EXISTS ${NACOS_DB} CHARACTER SET utf8 COLLATE utf8_general_ci;"

      NACOS_SQL_PATH="/opt/nacos2/conf/mysql-schema.sql"
      if [ -f "${NACOS_SQL_PATH}" ]; then
          mysql --socket=${MYSQL_SOCKET} -uroot -p${MYSQL_ROOT_PASSWORD} ${NACOS_DB} < ${NACOS_SQL_PATH}
      fi

      echo "Nacos 数据库初始化完成"

      # ========== 关闭临时 MySQL ==========
      echo "关闭临时 MySQL..."
      kill $MYSQL_PID
      wait $MYSQL_PID 2>/dev/null || true
fi

# ========== 最终交由 supervisord 管理 ==========
echo "初始化完成，交由 supervisord 管理"
exec "$@"
